package com.example.speeddating.config;
import com.example.speeddating.interceptor.AdminVerifyInterceptor;
import com.example.speeddating.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

@Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {

    @Autowired
    private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;

    @Autowired
    private LoginAdminHandlerMethodArgumentResolver loginAdminHandlerMethodArgumentResolver;


    @Autowired
    private TokenInterceptor tokenInterceptor;

    @Autowired
    private AdminVerifyInterceptor AdminVerifyInterceptor;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/image/**").addResourceLocations("file:D:\\soulmateImage\\");
//        registry.addResourceHandler("/image/**").addResourceLocations("file:/www/wwwroot/soulmate/soulmateImage/");
    }



    /**
     * 跨域
     * @param registry
     */

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*")
//                .allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true);
    }


    /**
     * 异步请求配置
     */
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }

    /**
     * 资源过滤
     */

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //排除拦截
        excludePath.add("/**/login/**");  //登录 请求放行
        excludePath.add("/article/recommendList");
        excludePath.add("/article/list");
//        excludePath.add("/comment/detail");
        excludePath.add("/profession/all");
        excludePath.add("/tags/all");
        excludePath.add("/topic/hot_topics");
        excludePath.add("/topic/all");
//        excludePath.add("/recommended_courses");
//        excludePath.add("/admin/**");
//        excludePath.add("/logout");
//        excludePath.add("/user_logout");
//        全局拦截器
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns(excludePath);
//        局部拦截管理员请求
//        registry.addInterceptor(AdminVerifyInterceptor).addPathPatterns(("/admin/**"));

        WebMvcConfigurer.super.addInterceptors(registry);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
//        实现LoginUser注解，从请求request中得到参数
        resolvers.add(loginUserHandlerMethodArgumentResolver);
        resolvers.add(loginAdminHandlerMethodArgumentResolver);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter());
    }
}
